package com.java8.stream;

import com.google.common.collect.Lists;
import com.java8.stream.bean.Dish;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Comparator.comparing;

/**
 * @author tuzhijin
 * @version Id: StreamOperation.java, v 0.1 2021/5/23 下午1:12 tuzhijin Exp $$
 */
public class StreamOperation {
    public static List<Dish> menu = Arrays.asList(
            new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT),
            new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("french fries", true, 530, Dish.Type.OTHER),
            new Dish("rice", true, 350, Dish.Type.OTHER),
            new Dish("season fruit", true, 120, Dish.Type.OTHER),
            new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 300, Dish.Type.FISH),
            new Dish("salmon", false, 450, Dish.Type.FISH));
    public static List<String> words = Arrays.asList("Hello", "World");
    public static List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

    public static void main(String[] args) {

//        test1();
//        test2();
//        test3();
//        test5();
//        test6();
//        test7();
//        test8();
        test9();
    }

    private static void test9() {
        // 构建流
        final List<String> list = Stream.of("Java 8 ", "Lambdas ", "In ", "Action").collect(Collectors.toList());
        list.stream().map(String::toUpperCase).forEach(System.out::println);
        int[] numbers = {2, 3, 5, 7, 11, 13};
        int sum = Arrays.stream(numbers).sum();
        System.out.println(sum);
    }

    private static void test8() {
        int calories = menu.stream()
                .mapToInt(Dish::getCalories)
                .sum();
        System.out.println(calories);
    }

    private static void test7() {
        // 求和
        // int sum = numbers.stream().reduce(0, Integer::sum);
        // 最大值
        int sum = numbers.stream().reduce(0, Integer::max);
        System.out.println(sum);
    }

    private static void test6() {
        menu.stream()
                .filter(Dish::isVegetarian)
                .findAny()
                .ifPresent(d -> System.out.println(d.getName()));
    }

    private static void test5() {
        words.stream()
                .map(word -> word.split(""))
                // 让个数组变成一个单独的流
                // .map(Arrays::stream)
                // 将各个生成流扁平化为单个流
                .flatMap(Arrays::stream)
                .distinct()
                .collect(Collectors.toList());
        // Hello World
        words.forEach(System.out::println);
    }

    private static void test3() {
        List<Integer> dishNameLengths = menu.stream()
                .map(Dish::getName)
                // 获取菜名的长度
                .map(String::length)
                .collect(Collectors.toList());
        dishNameLengths.forEach(System.out::println);
    }

    private static void test2() {
        List<String> title = Arrays.asList("Java8", "In", "Action");
        // 流只能操作一次，操作后就关闭了
        Stream<String> s = title.stream();
        s.forEach(System.out::println);
        s.forEach(System.out::println);
    }

    private static void test1() {
        // 结果存在另一个list
        List<String> threeHighCaloricDishNames = menu.stream()
                // 过滤出满足条件的
                .filter(d -> d.getCalories() > 300)
                // 获取名字
                .map(Dish::getName)
                .sorted()
                // 与limit相对应的是skip
                // .skip(2)
                .limit(3)
                .collect(Collectors.toList());

        threeHighCaloricDishNames.forEach(System.out::println);
    }

}
